# DEFINE PARAMETERS
years <- c(1999:2017)
slope_temp <- c("separate")
order_temp <- c(1)

############

# D1, D4, D5, D6
source('scripts/createDataset.R')
party_temp <- "partyUNION"
df_dur <- df_dur %>% select(-y) %>% rename(y = party_temp)

# LINEAR
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_reg_lm(slope = slope_temp, bw = bw, order = order_temp,
                                       covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName <- paste0('results/APPENDIX_D/D4&5&6.html')
names(reg) <- seq(1, length(reg), 1) %>% paste0('(', ., ')')
info <- data.frame() %>% 
  rbind(c('survey period', unlist(lapply(reg, function(x) x$param)) %>% as.character()))
modelsummary(reg, vcov = robust_temp, exponentiate = F, 
             estimate = "{estimate}{stars}", fmt = 3, stars = stars, 
             coef_map = coef_map, gof_map = gof_map, 
             notes = list(note2, note1), output = fileName)
fileName_plot <- paste0('results/APPENDIX_D/D1_1.png')
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = F) %>%
  createATTPlotDuration(., yintercept = 0) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

# NON-PARAMETRIC
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_reg_np(slope = slope_temp, bw = bw,
                                       covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]] <- reg[[j]][['RDDslot']][['model']]
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D1_2.png')
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = F) %>%
  createATTPlotDuration(., yintercept = 0, ylimits = c(-0.25, 0.25)) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

# LOGIT
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_gen_reg(slope = slope_temp, bw = bw, order = order_temp,
                                        fun = glm, family = binomial(link = 'logit'),
                                        covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D1_3.png')
if(party_temp == 'partySPD'){ylimmax <- c(0.25, 1.75)}else{ylimmax <- c(0.25, 2.5)}
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = T) %>%
  createATTPlotDuration(., ybreaks = seq(-5.5, 5.5, 0.25), ylimits = ylimmax, yintercept = 1) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

############

# D2, D7, D8, D9
source('scripts/createDataset.R')
party_temp <- "partyGRUNE"
df_dur <- df_dur %>% select(-y) %>% rename(y = party_temp)

# LINEAR
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_reg_lm(slope = slope_temp, bw = bw, order = order_temp,
                                       covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName <- paste0('results/APPENDIX_D/D7&8&9.html')
names(reg) <- seq(1, length(reg), 1) %>% paste0('(', ., ')')
info <- data.frame() %>% 
  rbind(c('survey period', unlist(lapply(reg, function(x) x$param)) %>% as.character()))
modelsummary(reg, vcov = robust_temp, exponentiate = F, 
             estimate = "{estimate}{stars}", fmt = 3, stars = stars, 
             coef_map = coef_map, gof_map = gof_map, 
             notes = list(note2, note1), output = fileName)
fileName_plot <- paste0('results/APPENDIX_D/D2_1.png')
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = F) %>%
  createATTPlotDuration(., yintercept = 0) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

# NON-PARAMETRIC
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_reg_np(slope = slope_temp, bw = bw,
                                       covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]] <- reg[[j]][['RDDslot']][['model']]
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D2_2.png')
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = F) %>%
  createATTPlotDuration(., yintercept = 0, ylimits = c(-0.25, 0.25)) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

# LOGIT
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_gen_reg(slope = slope_temp, bw = bw, order = order_temp,
                                        fun = glm, family = binomial(link = 'logit'),
                                        covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D2_3.png')
if(party_temp == 'partySPD'){ylimmax <- c(0.25, 1.75)}else{ylimmax <- c(0.25, 2.5)}
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = T) %>%
  createATTPlotDuration(., ybreaks = seq(-5.5, 5.5, 0.25), ylimits = ylimmax, yintercept = 1) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

############

# D3
source('scripts/createDataset.R')
party_temp <- "partySPD"
df_dur <- df_dur %>% select(-y) %>% rename(y = party_temp)

# LINEAR
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_reg_lm(slope = slope_temp, bw = bw, order = order_temp,
                                       covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D3_1.png')
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = F) %>%
  createATTPlotDuration(., yintercept = 0) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

# NON-PARAMETRIC
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_reg_np(slope = slope_temp, bw = bw,
                                       covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]] <- reg[[j]][['RDDslot']][['model']]
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D3_2.png')
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = F) %>%
  createATTPlotDuration(., yintercept = 0, ylimits = c(-0.25, 0.25)) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)

# LOGIT
reg <- list()
for(j in 1:length(years)){
  dat_temp1 <- defineDatasetDuration()
  reg[[j]] <- dat_temp1 %>% rdd_gen_reg(slope = slope_temp, bw = bw, order = order_temp,
                                        fun = glm, family = binomial(link = 'logit'),
                                        covariates = contVarBase %>% paste(., collapse = '+'))
  reg[[j]]$param <- paste(years[j], years[j]+2, sep = "-")
}
fileName_plot <- paste0('results/APPENDIX_D/D3_3.png')
if(party_temp == 'partySPD'){ylimmax <- c(0.25, 1.75)}else{ylimmax <- c(0.25, 2.5)}
extractRegResultsForPlot(reg, robust_temp = robust_temp, exponentiate = T) %>%
  createATTPlotDuration(., ybreaks = seq(-5.5, 5.5, 0.25), ylimits = ylimmax, yintercept = 1) %>% 
  ggsave(fileName_plot, plot = ., width = 7, height = 2.5)


